package com.china08.yxyapi.api.manage.stu.mystudy.homework.workreport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.stu.mystudy.homework.dowork.impl.HomeworkServiceImpl;

/***
 * 作业提交 2017 年3月11日09:15:54**
 * 
 * @author lys
 *
 */
@CrossOrigin
@RestController
public class MassageWorkSaveYueduworkController extends YxyApiBaseController {
    @Autowired
    HomeworkServiceImpl homeworkSrevice;

    @Autowired
    private HomeworkTaskReportRepository htrr;

    /**
     * 通知类作业学生点击提交按钮后的操作 2017年3月11日09:27:11
     *
     * @param reqModel
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/workreport/massagesaveyueduwork", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        RespModel respModel = new RespModel();
        // 得到参数并验证
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.studentId, null), "作业的ID不能为空");
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.taskId, null), "的ID不能为空");
        // 验证是否有提交权限
        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId, reqModel.taskId);
        /* 作业完成情况:-1-逾期未提交,0-未做,1-完成 ,2-已批阅,3-答题中 */
        if (homeworkTaskReport.getStatusOfFinished() == 3 || homeworkTaskReport.getStatusOfFinished() == 0) {
            if (homeworkSrevice.saveHomeworkForYD(reqModel.studentId, reqModel.quesId, reqModel.duration,
                    reqModel.lookTime, reqModel.taskId, reqModel.index)) {
            }
            respModel.saveResultats = "作业已保存";
        } else if (homeworkTaskReport.getStatusOfFinished() == 1 || homeworkTaskReport.getStatusOfFinished() == 2) {
            respModel.saveResultats = "作业已提交";
        } else if (homeworkTaskReport.getStatusOfFinished() == -1) {
            respModel.saveResultats = "作业已逾期";
        }
        return respModel;
    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
        public String saveResultats;
    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 任务ID
        public String taskId;
        // 学生ID
        public String studentId;
        // 作业ID
        public String quesId;
        // 做题时间
        public Integer duration;
        // 阅读时长
        public Integer lookTime;
        // 当前阅读的第几个文档
        public Integer index;
    }

}
